﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using test.Model;
using System.IO;
using Newtonsoft.Json;
using System.Threading;

namespace test
{
    public class SHowUiEventArgs : EventArgs
    {
        public bool judgeException;
        public OnlyDeviceId obj = new OnlyDeviceId();
        public SHowUiEventArgs(OnlyDeviceId obj, bool judgeException)
        {
            this.obj = obj;
            this.judgeException = judgeException;
        }
    }
    class UpDataUi
    {
        public delegate void SHowUiArgs(object sender, SHowUiEventArgs e);
        public SHowUiArgs showUiArgs;
        public void createOnlyDeviceId()
        {
            char[] a = {
                           'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
                           'q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J',
                           'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4',
                           '5','6','7','8','9','0'
                       };
            int c = 1;
            int count = 1000;
            Form1 form = new Form1();
            Random rand = new Random();     //定义随机变量
            OnlyDeviceId obj = new OnlyDeviceId();              //
            int basicInforLine = File.ReadAllLines(@"..\..\data\手机信息汇总.csv").Length;    //获取文件行数
            int idLine = File.ReadAllLines(@"..\..\data\ID.csv").Length;
            int keyallLine = File.ReadAllLines(@"..\..\data\高德keyall.csv").Length;
            StreamReader sr = new StreamReader(@"..\..\data\手机信息汇总.csv");
            StreamReader choose_id = new StreamReader(@"..\..\data\ID.csv");
            StreamReader choose_keyall = new StreamReader(@"..\..\data\高德keyall.csv");
            bool judgeException=false;
            for (int j = 0; j < count; j++)
            {
                judgeException = false;
                StreamWriter sw = new StreamWriter(@"..\..\createDeviceID\\device" + (c++) + ".id");
                try
                {
                    string basicInfor = "", id = "", keyall = "";
                    int time = rand.Next(1, basicInforLine - 1);
                    for (int i = 0; i < time; i++)
                        sr.ReadLine();
                    basicInfor = sr.ReadLine();
                    sr.BaseStream.Seek(0, new SeekOrigin());
                    time = rand.Next(1, idLine - 1);
                    for (int i = 0; i < time; i++)
                        choose_id.ReadLine();
                    id = choose_id.ReadLine();
                    choose_id.BaseStream.Seek(0, new SeekOrigin());
                    string utdid = "", mac = "", subscriberid = "4600";
                    for (int i = 0; i < 24; i++)        //生成24位随机码
                    {
                        time = rand.Next(0, a.Length - 1);
                        utdid += a[time];
                        if (i < 11)             //生成subscriberid
                        {
                            time = rand.Next(52, a.Length - 1);
                            subscriberid += a[time];
                        }
                    }
                    for (int i = 0; i < 12; i++)         //生成mac
                    {
                        time = rand.Next(26, a.Length - 1);
                        mac += a[time];
                        if (i == 11)
                            break;
                        if (i % 2 == 1)
                            mac += ':';
                    }
                    string[] xu = new string[20];
                    xu = id.Split(',');
                    obj.deviceID = xu[1];
                    xu = basicInfor.Split(',');
                    obj.Manufacturer = xu[1];
                    obj.deviceModel = xu[2];
                    obj.Release = xu[3];
                    if (xu[4] == null)
                        xu[4] = "";
                    obj.sdkversion = xu[4];
                    obj.resolution = xu[5];
                    if (xu[6] == null)
                        xu[6] = "";
                    obj.device = xu[6];
                    obj.subscriberid = subscriberid;
                    obj.mac = mac;
                    obj.utdid = utdid;
                    obj.networkType = "13";
                    obj.networkOperatorName = getNetworkOperatorName(subscriberid, obj.Manufacturer);
                    obj.networkOperator = "0";
                    obj.networkOperator += subscriberid[4];
                    time = rand.Next(0, 9);
                    obj.appversion = string.Format("{0:G}", time);
                    time = rand.Next(1, keyallLine - 1);
                    for (int i = 0; i < time; i++)
                        choose_keyall.ReadLine();
                    keyall = choose_keyall.ReadLine();
                    choose_keyall.BaseStream.Seek(0, new SeekOrigin());
                    xu = keyall.Split(',');
                    obj.sha1 = xu[1];
                    obj.applabel = xu[2];
                    obj.packagename = xu[4];
                    obj.apikey = xu[5];
                    obj.proxy = "";
                    sw.Write(JsonConvert.SerializeObject(obj));
                }
                catch
                {
                    judgeException = true;
                }
                finally
                {
                    sw.Close();
                    Thread.Sleep(230);                  
                    SHowUiEventArgs e = new SHowUiEventArgs(obj, judgeException);
                    showUiArgs(null, e);
                }
            }
            sr.Close();
            choose_id.Close();
            choose_keyall.Close();

        }
        public string getNetworkOperatorName(string num, string Manufacturer)
        {
            if (judgeManufacturer(Manufacturer))
            {
                if (num[4] == '0' || num[4] == '2' || num[4] == '7' || num[4] == '8')
                    return "中国移动";
                else if (num[4] == '1' || num[4] == '6' || num[4] == '9')
                    return "中国联通";
                else
                    return "中国电信";
            }
            else
            {
                if (num[4] == '0' || num[4] == '2' || num[4] == '7' || num[4] == '8')
                    return "CMCC";
                else if (num[4] == '1' || num[4] == '6' || num[4] == '9')
                    return "CUCC";
                else
                    return "CTCC";
            }
        }
        public bool judgeManufacturer(string Manufacturer)
        {
            if (Manufacturer == "Xiaomi" || Manufacturer == "OPPO")
                return true;
            else
                return false;
        }
    }
}
